package org.polaris.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.polaris.gift.client.AuthClient;
import org.polaris.gift.constants.ResponseCode;
import org.polaris.gift.domain.LoginUser;
import org.polaris.gift.domain.User;
import org.polaris.gift.domain.UserInfo;
import org.polaris.gift.dto.UserPhoneRegisterDto;
import org.polaris.gift.exception.PolarisAssert;
import org.polaris.gift.mapper.UserInfoMapper;
import org.polaris.gift.mapper.UserMapper;
import org.polaris.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.polaris.gift.service.IVerifyCodeService;
import org.polaris.gift.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author polaris
 * @since 2023-06-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private AuthClient authClient;

    @Override
    public AjaxResult sendRegisterVerifyCode(String phone) {
        //1 校验用户是否存在
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", phone));
        PolarisAssert.isNull(users, ResponseCode.RESPONSE_CODE_400014);
        //2 发送短信验证码
        return verifyCodeService.sendRegisterVerifyCode(phone);
    }
    @Override
    public AjaxResult registerByPhone(UserPhoneRegisterDto dto) {

        //1 校验
        //1.1 校验验证码
        String redisVerifyCode = verifyCodeService.getRegisterVerifyCode(dto.getPhone());
        PolarisAssert.isNotBlank(redisVerifyCode,ResponseCode.RESPONSE_CODE_400004);
        String inputVerifyCode = dto.getValidateCode();
        PolarisAssert.isEqIgnoreCase(redisVerifyCode,inputVerifyCode,ResponseCode.RESPONSE_CODE_400004);
        //2 远程调用授权中心添加loginUser返回LoginId
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setPassword(dto.getPassword());
        loginUser.setTel(dto.getPhone());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.getSuccess())
            return ajaxResult;

        Long loginId = Long.valueOf(ajaxResult
                .getResultObj().toString());

        //3 保存用户信息  基本信息 详情信息---设置loginId
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setPhone(dto.getPhone());
        user.setPassword(dto.getPassword());
        user.setSalt(null);
        user.setBitState(1L);
        user.setSecLevel(1);
        user.setLoginId(loginId);
        userMapper.insert(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setRegTime(new Date().getTime());
        userInfo.setLevel(0);
        userInfo.setGrowScore(0);
        userInfo.setUserId(user.getId());
        userInfoMapper.insert(userInfo);
        //4 通过mq发送注册成功消息  @TODO
        return AjaxResult.me();
    }
}
